package com.luo.demo;

import com.alibaba.fastjson.JSON;
import com.luo.mapper.UserMapper;
import com.luo.pojo.User;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/managePersonServlet0")
public class HomePageManagePerson0 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String manageId = req.getParameter("id");
        SqlSession sqlSession = util00.useSqlSessionFactory().openSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);

        if (manageId.equals("0")){
            List<User> allStudents = mapper.selectAllStudents();
            String jsonStr = JSON.toJSONString(allStudents);
            resp.getWriter().write(jsonStr);
            sqlSession.close();
        }else if (manageId.equals("1")){
            List<User> allTeachers = mapper.selectAllTeachers();
            String jsonStr = JSON.toJSONString(allTeachers);
            resp.getWriter().write(jsonStr);
            sqlSession.close();
        }else{
            resp.getWriter().write("err");
            sqlSession.close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
